package gen.tools.host;

import gen.tools.Widgets;
import gen.tools.jqGrid.ResultTableSet;
import gen.tools.jqGrid.Rows;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

public class QueryHost extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public QueryHost() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		
		final Widgets wid = new Widgets();
		request.setCharacterEncoding( wid.UIcodeSet );
		response.setContentType("application/json");
		response.setCharacterEncoding( wid.UIcodeSet );
		
		Connection conn = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		
		String sql = "";
		
		final ResultTableSet resultTableSet = new ResultTableSet();
		final String subsysId = request.getParameter("subsysId");
		
		sql = "SELECT COUNT(*) FROM hosts";
		
		String sord = request.getParameter("sord");
		String sidx =  request.getParameter("sidx");
		
		int total_pages = 1;
		int count  = 0; 
		int page = 1;
		int limit = 10;
		
		if ("".equalsIgnoreCase(sidx)|| sidx==null) {
			sidx = "ip";
		} 
		if ("".equalsIgnoreCase(sord)|| sord==null) {
			sord = "ASC";
		} else {
			page = Integer.parseInt(request.getParameter("page"));
			limit = Integer.parseInt(request.getParameter("rows"));
		}
		
		
		
		try {
			conn = wid.getConn();	
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			rs.next();
			count  = rs.getInt(1);
			if ( count  != 0 ) {
				
				if (limit  < count) {
					total_pages = (count / limit)+1;	
				}
				sql="SELECT ip,hostname,port,loginId,loginPWD FROM hosts WHERE subsysId=?";
				ps = conn.prepareStatement(sql);
				ps.setString(1,subsysId);
				rs = ps.executeQuery();
				final ArrayList<Rows> rows = new ArrayList<Rows>();
				while ( rs.next() ) {
					final Rows row = new Rows();
					final String[] cell = new String[4];
					row.setId( rs.getString("ip") );
					cell[0] = rs.getString("ip");
					cell[1] = String.valueOf( rs.getInt("port") ) ;
					cell[2] = rs.getString("loginId");
					cell[3] = rs.getString("loginPWD");
					row.setCell(cell);
					rows.add(row);	
				}
				resultTableSet.setRows( rows );
				resultTableSet.setPage( String.valueOf(page) );
				resultTableSet.setTotal( total_pages );
				resultTableSet.setRecords( String.valueOf(count) );
				
			} else {
				page =0;
				total_pages=0;
				resultTableSet.setPage( String.valueOf(page) );
				resultTableSet.setTotal( total_pages );
				resultTableSet.setRecords( String.valueOf(count) );
			}
		} catch ( Exception e) {
			wid.errLog(e);
		} finally {
			try {
				if (rs != null) {rs.close();}
				if (ps!= null) {ps.close();}
				if (conn!= null) {conn.close();}
			} catch (SQLException e) { System.out.println(wid.errProcess(e)); }
		}
		response.getWriter().write(new Gson().toJson(resultTableSet));	
	}
}
